const { Sequelize, Model, DataTypes } = require("sequelize");
const fs = require('fs');
const path = require('path');


// 方法 3: 分别传递参数 (其它数据库)
const sequelize = new Sequelize('tw', 'root', 'root', {
  host: 'localhost',
  dialect: 'mysql'
});

let databases = {}
async function initSql() {
  const directoryPath = './test'; // 替换为您的目录路径
  fs.readdir(directoryPath, (err, files) => {
    if (err) {
      console.error('Error reading directory:', err);
      return;
    }

    files.forEach(async (file) => {
      if (path.extname(file) === '.js') {
        const moduleName = path.basename(file, '.js');
        databases[moduleName] = require(`./${directoryPath}/${file}`)(sequelize, DataTypes)
        // const module = require(`./${directoryPath}/${file}`); // 相对路径，根据需要调整

        // 在这里使用模块
        // module.doSomething();
      }
      // await sequelize.sync({ force: true });
    });


    global.database = databases

    // database.department.belongsTo(database.user, { foreignKey: 'dept_id' });
    database.user.belongsTo(database.department, { foreignKey: 'dept_id' });
    database.user.belongsTo(database.identity, { foreignKey: 'qx' });

  });
}
initSql()
module.exports = databases;
